﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using System.IO;
using System.Text;

using CE.User;
using CE.Global;
using CE.Provider;
using CE.Database;
using CE.CarInsurance;

public partial class Provider_ProposalForm_DownloadProposalForm : System.Web.UI.Page
{
    string PageName = "CXDPF";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null || Session[CE.Global.General.ProviderCompanyId] == null)
        {
            Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        }
        CheckMainPageAccess();

        if (!IsPostBack)
        {
            CheckSubPageAccessByInsuranceType();
            ddlDay1.Visible = false;
            lblFromDate.Text = "Date :";
            lblToDate.Visible = false;
            ddlDay2.Visible = false;
            ddlMonth2.Visible = false;
            ddlYear2.Visible = false;

            ddlYear1.Items.Clear();
            ddlYear2.Items.Clear();
            int _year = DateTime.Now.Year;
            ddlYear1.Items.Add(new ListItem(_year.ToString(), _year.ToString()));
            ddlYear2.Items.Add(new ListItem(_year.ToString(), _year.ToString()));
            _year = _year - 1;
            ddlYear1.Items.Add(new ListItem(_year.ToString(), _year.ToString()));
            ddlYear2.Items.Add(new ListItem(_year.ToString(), _year.ToString()));

            //ddlDay1.Text = "00";
            ddlDay1.Text = DateTime.Now.Day.ToString("00");
            ddlMonth1.Text = DateTime.Now.Month.ToString("00");
            ddlYear1.Text = DateTime.Now.Year.ToString("0000");
            //ddlDay2.Text = "00";
            ddlDay2.Text = DateTime.Now.Day.ToString("00");
            ddlMonth2.Text = DateTime.Now.Month.ToString("00");
            ddlYear2.Text = DateTime.Now.Year.ToString("0000");

            ImageButton1_Click(this, null);

            if (Session[CE.Global.General.ProviderCompanyId].ToString() == "23")
            {
                ddlFileType.Visible = true;
            }
        }
    }

    protected void ImageButton1_Click(object sender, EventArgs e)
    {
        
        txtReportDate1.Text = "";
        txtReportDate2.Text = DateTime.Now.Year.ToString("0000") + DateTime.Now.Month.ToString("00") + DateTime.Now.Day.ToString("00");
        txtReportDate1.Text = ddlYear1.Text;
        txtReportDate1.Text += ddlMonth1.Text;
        if (ddlReportType.SelectedIndex > 0)
        {
            if (ddlDay1.SelectedIndex > 0)
            {
                txtReportDate1.Text += ddlDay1.Text;
            }
        }
        if (ddlReportType.SelectedIndex == 2)
        {
            txtReportDate2.Text = ddlYear2.Text;
            txtReportDate2.Text += ddlMonth2.Text;
            txtReportDate2.Text += ddlDay2.Text;
        }

        ProposalDownloadDB _proposaldb = new ProposalDownloadDB();
        DataSet dsProposal = new DataSet();

        string ProviderCompanyId = Session[CE.Global.General.ProviderCompanyId].ToString();
        Int16 _ReportType = Convert.ToInt16(ddlReportType.Text);
        string _ReportDate1 = txtReportDate1.Text.Trim();
        string _ReportDate2 = txtReportDate2.Text.Trim();
       
        //dsProposal = _proposaldb.GetAllProposal(ProviderCompanyId, ddlInsuranceType.Text, _ReportType, _ReportDate1, _ReportDate2);
        dsProposal = _proposaldb.GetAllProposal(ProviderCompanyId, "CI", _ReportType, _ReportDate1, _ReportDate2);
        grvResultViewProposal.DataSource = dsProposal;
        grvResultViewProposal.DataBind();
    
    }

    protected void ibtBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("~/Provider/ProviderHome.aspx");
    }

    protected void ConfirmDownload(object sender, ImageClickEventArgs e)
    {
        string refid = inputrefid.Value;
        ProposalDownloadDB _proposaldb = new ProposalDownloadDB();
        DataSet dsDownload = new DataSet();

        Int64 ProviderCompanyId = Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString());
        Int64 ResultId = 0;
        //string isconfirmdownload = "False";
        Boolean isconfirmdownload = false;

        if (ddlInsuranceType.Text == "CI")
        {
            dsDownload = _proposaldb.GetCarDownloadInfo(Convert.ToInt64(refid), Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()));
        }
        else if (ddlInsuranceType.Text == "TI")
        {
            dsDownload = _proposaldb.GetTIDownloadInfo(Convert.ToInt64(refid), Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()));
        }
        else if (ddlInsuranceType.Text == "PA")
        {
            dsDownload = _proposaldb.GetPADownloadInfo(Convert.ToInt64(refid), Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()));
        }

        if (dsDownload.Tables[0].Rows.Count > 0)
        {
            isconfirmdownload = Boolean.Parse(dsDownload.Tables[0].Rows[0]["IsConfirmDownload"].ToString());
        }

        Int64 UserId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());

        if (ddlInsuranceType.Text == "CI")
        {
            ResultId = _proposaldb.UpdateCarDownloadInfo(Convert.ToInt64(refid), ProviderCompanyId, true, isconfirmdownload, 1, 0, UserId);
        }
        else if (ddlInsuranceType.Text == "TI")
        {
            ResultId = _proposaldb.UpdateTIDownloadInfo(Convert.ToInt64(refid), ProviderCompanyId, true, isconfirmdownload, 1, 0, UserId);
        }
        else if (ddlInsuranceType.Text == "PA")
        {
            ResultId = _proposaldb.UpdatePADownloadInfo(Convert.ToInt64(refid), ProviderCompanyId, true, isconfirmdownload, 1, 0, UserId);
        }

        if (!isconfirmdownload && ProviderCompanyId != 30)
        {
            Response.Redirect("DownloadProposalConfirmation.aspx?pid=" + refid + "&type=" + ddlInsuranceType.Text + "&dl=" + ResultId.ToString());
        }


    }

    public string GetDownloadURl(string refid)
    {

        //if (Session[General.ProviderCompanyId].ToString() == "23")
        //{
        //    if (ddlInsuranceType.Text == "CI")
        //    {
        //        _html = GenerateCIHTML(Convert.ToInt64(refid.ToString()));
        //    }
        //}
        string _url = "";
        Session[General.InsType] = ddlInsuranceType.Text;
        string _refId = CE.Security.MySecurity.Encrypt(refid.ToString(), true);
        _url = "javascript:OpenProposal('" + refid.ToString() + "');";
        return _url;
    }

    public string GetPDFDownloadURl(string refid)
    {
        string _url = "";
        Session[General.InsType] = ddlInsuranceType.Text;
        string _refId = CE.Security.MySecurity.Encrypt(refid.ToString(), true);
        _url = "javascript:OpenProposalPDF('" + refid.ToString() + ",pdf');";
        return _url;
    }

    public string GetBoolValue(string bval)
    {
        string boolvalue = "No";

        if (bval == "True")
            boolvalue = "Yes";

        return boolvalue;
    }

    protected void ddlInsuranceType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.ProviderCompanyId].ToString() == "23")
        {
            if (ddlInsuranceType.Text == "CI")
            {
                ddlFileType.Visible = true;
            }
            else
            {
                ddlFileType.Visible = false;
            }
        }
        //if (Session[CE.Global.General.ProviderCompanyId].ToString() == "30")
        //{
        //    if (ddlInsuranceType.Text == "PA")
        //    {
        //      ddlSubmissionType.Visible = true;
        //    }
        //    else
        //    {
        //      ddlSubmissionType.Visible = false;
        //    }
        //}
    }

    protected void ddlReportType_SelectedIndexChanged(object sender, EventArgs e)
    {
        //ddlDay1.SelectedIndex = 0;
        //ddlDay2.SelectedIndex = 0;
        ddlDay1.Text = DateTime.Now.Day.ToString("00");
        ddlDay2.Text = DateTime.Now.Day.ToString("00");
        ddlMonth1.Text = DateTime.Now.Month.ToString("00");
        ddlMonth2.Text = DateTime.Now.Month.ToString("00");
        ddlYear1.Text = DateTime.Now.Year.ToString("0000");
        ddlYear2.Text = DateTime.Now.Year.ToString("0000");
        if (Convert.ToInt16(ddlReportType.Text) == 0)
        {
            ddlDay1.Visible = false;
            lblFromDate.Text = "Date :";
            lblToDate.Visible = false;
            ddlDay2.Visible = false;
            ddlMonth2.Visible = false;
            ddlYear2.Visible = false;
        }
        else if (Convert.ToInt16(ddlReportType.Text) == 1)
        {
            ddlDay1.Visible = true;
            lblFromDate.Text = "Date :";
            lblToDate.Visible = false;
            ddlDay2.Visible = false;
            ddlMonth2.Visible = false;
            ddlYear2.Visible = false;
        }
        else if (Convert.ToInt16(ddlReportType.Text) == 2)
        {
            lblFromDate.Text = "From Date :";
            lblToDate.Text = " To Date :";
            lblToDate.Visible = true;
            ddlDay2.Visible = true;
            ddlMonth2.Visible = true;
            ddlYear2.Visible = true;
            ddlDay1.Visible = true;
        }
    }

    protected void grvResultViewProposal_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        Boolean _DownLoadStatus = CheckSubPageAccess();
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton imgDownload = new ImageButton();
            imgDownload = (ImageButton)e.Row.FindControl("ibtnDownload");
            ImageButton imgPDFDownload = new ImageButton();
            imgPDFDownload = (ImageButton)e.Row.FindControl("ibtnPDFDownload");

            if (Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()) == 23 && ddlInsuranceType.Text == "CI")
            {
                if (ddlFileType.Text == "PDF")
                {
                    imgDownload.Visible = false;
                    imgPDFDownload.Visible = true;
                }
                else
                {
                    imgDownload.Visible = true;
                    imgPDFDownload.Visible = false;
                }
            }
            else
            {
                imgDownload.Visible = true;
                imgPDFDownload.Visible = false;
            }

            if (_DownLoadStatus == true)
            {
                DateTime datePosted = DateTime.Parse(DataBinder.Eval(e.Row.DataItem, "PostedDate").ToString());

                if (datePosted < DateTime.Now.AddDays(-30))
                {
                    imgDownload.Enabled = false;
                }
            }
            else
            {
                imgDownload.Enabled = false;
            }
        }
    }

    private void CheckMainPageAccess()
    {
        Int64 PageAccess = General.GetProviderMainPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), PageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
    }

    private void CheckSubPageAccessByInsuranceType()
    {
        Int64 ProviderCompanyId = Convert.ToInt64(Session[General.ProviderCompanyId].ToString());
        Int64 RefCEUserId = Convert.ToInt64(Session[General.UserId].ToString());
        MyDatabase _db = new MyDatabase();
        string _spName = "sprocProviderProposalDownloadSubPagesSelectItemListByUser";

        SortedList _spara = new SortedList();
        _spara.Add("@ProviderCompanyId", ProviderCompanyId);
        _spara.Add("@RefCEUserId", RefCEUserId);

        DataTable _dt = _db.GetDataTableBySP(_spName, _spara);
        if (_dt != null && _dt.Rows.Count > 0)
        {
            string[] _InsType = new string[_dt.Rows.Count];
            string[] _Access = new string[_dt.Rows.Count];
            for (int i = 0; i < _dt.Rows.Count; i++)
            {
                _InsType[i] = _dt.Rows[i]["CXSubPageName"].ToString();
                _Access[i] = _dt.Rows[i]["AccessLevel"].ToString();
            }
            int _InsTypeCount = 0;
            for (int j = 0; j < _InsType.Length; j++)
            {
                if (_Access[j].ToString() != "0")
                {
                    _InsTypeCount = _InsTypeCount + 1;
                }
            }
            if (_InsTypeCount == 0)
            {
                //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
                Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
            }
            ddlInsuranceType.Items.Clear();
            for (int k = 0; k < _InsType.Length; k++)
            {
                int _index = ddlInsuranceType.Items.Count;
                if (_InsType[k].ToString() == "CXDPFCI" && _Access[k].ToString() != "0")
                {
                    ddlInsuranceType.Items.Insert(_index, new ListItem("Car Insurance", "CI"));
                }
                else if (_InsType[k].ToString() == "CXDPFTI" && _Access[k].ToString() != "0")
                {
                    ddlInsuranceType.Items.Insert(_index, new ListItem("Travel Insurance", "TI"));
                }
                else if (_InsType[k].ToString() == "CXDPFPA" && _Access[k].ToString() != "0")
                {
                    ddlInsuranceType.Items.Insert(_index, new ListItem("Personal Accident", "PA"));
                }
            }
        }
        else
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
    }

    private bool CheckSubPageAccess()
    {
        Boolean _download = false;
        string _subPage = "";
        if (ddlInsuranceType.Text == "CI")
        {
            _subPage = "CXDPFCI";
        }
        else if (ddlInsuranceType.Text == "TI")
        {
            _subPage = "CXDPFTI";
        }
        else if (ddlInsuranceType.Text == "PA")
        {
            _subPage = "CXDPFPA";
        }
        Int64 PageAccess = General.GetProviderSubPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), _subPage);
        if (PageAccess == 400)
        {
            _download = true;
        }
        return _download;
    }

    protected void btnDownLoad_Click(object sender, EventArgs e)
    {
        
    }

    public void ExportGridView(string fileName, Label lblProposal, string headerText)
    {
        string attachment = "attachment; filename=" + fileName + ".xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        // Create a form to contain the grid
        HtmlForm frm = new HtmlForm();
        frm.InnerHtml = headerText;
        lblProposal.Parent.Controls.Add(frm);
        frm.Attributes["runat"] = "server";
        frm.Controls.Add(lblProposal);
        frm.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
    }

}
